package com.example.miniodemo.controller;

import com.example.miniodemo.domain.AjaxResult;
import com.example.miniodemo.util.MinioUtil;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.UUID;

@RestController
public class FileController {

    @Autowired
    MinioUtil minioUtil;

    @RequestMapping(value = "/uploadfile", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult fileupload(@RequestParam MultipartFile uploadfile, @RequestParam String bucket,
                                 @RequestParam(required=false) String objectName) throws Exception {
        minioUtil.createBucket(bucket);
        System.out.println(uploadfile.getContentType());
        String uuid = UUID.randomUUID().toString();
        if (objectName != null) {
            minioUtil.uploadFile(uploadfile.getInputStream(), bucket, objectName+"/"+uploadfile.getOriginalFilename());
        } else {
            minioUtil.uploadFile(uploadfile.getInputStream(), bucket, uuid+uploadfile.getOriginalFilename().substring(uploadfile.getOriginalFilename().lastIndexOf(".")));
        }
        return AjaxResult.success(uuid);
    }

    @RequestMapping(value = "/listBuckets", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult listBuckets() throws Exception {
        return AjaxResult.success(minioUtil.listBuckets());
    }

    @RequestMapping(value = "/listFiles", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult listFiles(@RequestParam String bucket) throws Exception {
        return AjaxResult.success("200", minioUtil.listFiles(bucket));
    }

    @RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
    @ResponseBody
    public void downloadFile(@RequestParam String bucket, @RequestParam String objectName,
                             HttpServletResponse response) throws Exception {
        InputStream stream = minioUtil.download(bucket, objectName);
        ServletOutputStream output = response.getOutputStream();
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(objectName.substring(objectName.lastIndexOf("/") + 1), "UTF-8"));
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("UTF-8");
        IOUtils.copy(stream, output);
    }

    /**
     * 浏览器直接预览图片 contentType 不可以设置 image/*  需要具体的 上传的时候可以把contentType存储下来 预览的时候读取设置
     * @param bucket
     * @param objectName
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/previewImage", method = RequestMethod.GET)
    @ResponseBody
    public void previewImage(@RequestParam String bucket, @RequestParam String objectName,
                             HttpServletResponse response) throws Exception {
        InputStream stream = minioUtil.download(bucket, objectName);
        ServletOutputStream output = response.getOutputStream();
        response.setContentType("image/jpeg");
        IOUtils.copy(stream, output);
    }


    @RequestMapping(value = "/deleteFile", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult deleteFile(@RequestParam String bucket, @RequestParam String objectName) throws Exception {
        minioUtil.deleteObject(bucket, objectName);
        return AjaxResult.success();
    }

    @RequestMapping(value = "/deleteBucket", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult deleteBucket(@RequestParam String bucket) throws Exception {
        minioUtil.deleteBucket(bucket);
        return AjaxResult.success();
    }
}
